//
//  ResetPwdViewController.m
//  EyeCare
//
//  Created by 韩盼盼 on 2016/12/22.
//  Copyright © 2016年 HG. All rights reserved.
//

#import "ResetPwdViewController.h"
#import "LoginViewController.h"
@interface ResetPwdViewController ()<UITextFieldDelegate>
{
    UITextField *textField_newpwd;
    UITextField *textField_cnewpwd;
    UIView *contentViews;
    UIButton *nextBtn;

}
@property (nonatomic,strong) NSMutableArray *mutable_checks;

@end

@implementation ResetPwdViewController
-(NSMutableArray *)mutable_checks{
    if (!_mutable_checks) {
        _mutable_checks=[[NSMutableArray alloc]initWithObjects:@"0",@"0", nil];
    }
    return _mutable_checks;
}
- (void)viewDidLoad {
//    self.navBarType = NO;
    [super viewDidLoad];
    self.view.backgroundColor = HGCOLOR(@"f6f6f6");
    [self updateNav];
    [self initWithSubViews];
}
#pragma mark - draw views
-(void)updateNav
{
//    [self.navigateBar.leftBtn setHidden:NO];
//    
//    [self.navigateBar.bgImageView setBackgroundColor:[UIColor whiteColor]];
//    [self.navigateBar.backImg setImage:[UIImage imageNamed:@"left_arrow_blue"]];
    [self setTitle:@"重置密码"];
//    self.navigateBar.titleLabel.text = @"重置密码";
//    self.navigateBar.titleLabel.textColor = RGBCOLOR(51, 51, 51);
//    
//    self.navigateBar.titleLabel.whc_CenterX(0).whc_Height(44).whc_TopSpace(20);
//    [self.navigateBar.lineImageView setHidden:NO];
}

//-(void)updateNav
//{
//    [self.navigateBar.bgImageView setBackgroundColor:HGCOLOR(@"ffffff")];
//    [self.navigateBar.lineImageView setHidden:YES];
//    self.navigateBar.titleLabel.textColor = HGCOLOR(@"b00000");
//    self.navigateBar.titleLabel.text = @"重置密码";
//    [self.navigateBar.leftBtn setImage:[UIImage imageNamed:@"left_b_back"] forState:UIControlStateNormal];
//}

-(void)initWithSubViews{
    contentViews =[[UIView alloc]initWithFrame:CGRectMake(0, 64+HGADPT(5), __kScreenWidth , __kScreenHeight - 64 - HGADPT(5))];
    contentViews.backgroundColor = HGCOLOR(@"ffffff");
    [self.view addSubview:contentViews];
    
    
    UILabel *alertlab = [[UILabel alloc]initWithFrame:CGRectMake(HGADPT(48), HGADPT(57), __kScreenWidth - 2*HGADPT(48), HGADPT(25))];
    alertlab.text = @"请重新设置你的登录密码：";
    alertlab.textColor = HGCOLOR(@"fe7200");
    alertlab.font=HGFont(24);
    [contentViews addSubview:alertlab];
    
    textField_newpwd = [[UITextField alloc]initWithFrame:CGRectMake(MARGIN_LEFT, alertlab.bottom+HGADPT(20), __kScreenWidth - 2*MARGIN_LEFT , HGADPT(98))];
    textField_newpwd.placeholder =@"新的登录密码";
    textField_newpwd.tintColor = BTN_NORMAL_COLOR;
    textField_newpwd.backgroundColor = HGCOLOR(@"ffffff");
    textField_newpwd.layer.borderColor = HGCOLOR(@"e1e1e1").CGColor;
    textField_newpwd.layer.borderWidth = HGADPT(1);
    textField_newpwd.clipsToBounds = YES;
    textField_newpwd.leftView = [self getLeftView:@"login_userpwd"];
    textField_newpwd.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField_newpwd.leftViewMode = UITextFieldViewModeAlways;
    [textField_newpwd setValue:@"12" forKey:@"LimitInput"];
    textField_newpwd.layer.cornerRadius = 10 * ADPT;
    textField_newpwd.font = HGFont(30);
    textField_newpwd.delegate= self;
    textField_newpwd.tag = 120+0;

    [contentViews addSubview:textField_newpwd];
    
    textField_cnewpwd = [[UITextField alloc]initWithFrame:CGRectMake(MARGIN_LEFT, textField_newpwd.bottom+HGADPT(30), __kScreenWidth - 2*MARGIN_LEFT , HGADPT(98))];
    textField_cnewpwd.placeholder =@"确认新的登录密码";
    textField_cnewpwd.tintColor = BTN_NORMAL_COLOR;
    textField_cnewpwd.backgroundColor = HGCOLOR(@"ffffff");
    textField_cnewpwd.layer.borderColor = HGCOLOR(@"e1e1e1").CGColor;
    textField_cnewpwd.layer.borderWidth = HGADPT(1);
    textField_cnewpwd.clipsToBounds = YES;
    textField_cnewpwd.leftView = [self getLeftView:@"login_userpwd"];
    textField_cnewpwd.leftViewMode = UITextFieldViewModeAlways;
    textField_cnewpwd.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField_cnewpwd.secureTextEntry =YES;
    textField_cnewpwd.tag = 120+1;
    textField_cnewpwd.delegate= self;
    [textField_cnewpwd setValue:@"12" forKey:@"LimitInput"];
    textField_cnewpwd.layer.cornerRadius = 10 * ADPT;
    textField_cnewpwd.font = HGFont(30);
    [contentViews addSubview:textField_cnewpwd];
    
    nextBtn =[[UIButton alloc]initWithFrame:CGRectMake(MARGIN_LEFT, textField_cnewpwd.bottom+HGADPT(50), __kScreenWidth - 2*MARGIN_LEFT, HGADPT(98))];
    [nextBtn setTitle:@"确定" forState:UIControlStateNormal];
    [nextBtn setTitleColor:HGCOLOR(@"ffffff") forState:UIControlStateNormal];
    nextBtn.layer.cornerRadius = 10 * ADPT;
    nextBtn.titleLabel.font = HTFont(36 * ADPT);
    nextBtn.backgroundColor = HGColor(221, 221, 221);
    nextBtn.userInteractionEnabled = NO;
    [nextBtn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentViews addSubview:nextBtn];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void)nextAction:(UIButton *)btn
{
    [self.view endEditing:YES];

    [self resetPwd];
}

-(void)resetPwd
{
    /*
     captcha	√	string	验证码
     mobile	√	string	手机号
     newpassword	√	string	新密码，sha1加密过的
     */
    
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:self.user_captch forKey:@"captcha"];
//    [params setObject:self.user_tel forKey:@"mobile"];
//    [params setObject:[textField_cnewpwd.text sha1] forKey:@"newpassword"];
//    
//    WS(ws);
//    NSString *url = API_LHF_BASE_URL(MEMBER_PASSWORD_FIND_USEMOBILE);
//    [HttpRequest requestWhtiURLString:url type:HttpRequestTypePost parameters:params success:^(id obj) {
//        BOOL issuccess = [[obj objectForKey:@"issuccess"] boolValue];
//        if (issuccess) {
//            for (id vc in self.navigationController.viewControllers) {
//                if ([vc isKindOfClass:[LoginViewController class]]) {
//                    [ws.navigationController popToViewController:vc animated:YES];
//                    return;
//                }
//            }
//        }
//    } failure:^(NSError *error) {
//        [ws.view makeToast:error.description];
//    } waitView:ws.view afterDely:0 vc:ws];
    
    
//    [[HttpRequest shardWebUtil]postNetworkRequestURLString:API_BASE_URL(MEMBER_PASSWORD_FIND_USERMOBILE) parameters:params success:^(id obj) {
//        NSLog(@"修改密码：：：：：：：：%@",obj);
//        NSDictionary *data=obj[@"data"];
//        NSString *errorstr=obj[@"error"];
//        if ([errorstr isEqualToString:@""]) {
//            if ([data[@"issuccess"] intValue]==1) {
//                [MBProgressHUD showSuccess:@"密码重置成功"];
//                [self viewJumpNext:@"LoginViewController"];
//            }
//        }
//        else
//        {
//            [MBProgressHUD showSuccess:errorstr];
//        }
//    } fail:^(NSError *error) {
//        
//    }];

}

-(UIView *)getLeftView:(NSString *)imgstr{
    UIView *leftView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, HGADPT(88), HGADPT(98))];
    UIImage *image=[UIImage imageNamed:imgstr];
    
    UIImageView *lefticon=[[UIImageView alloc]initWithFrame:CGRectMake(HGADPT(43),(leftView.height- HGADPT(image.size.height))/2 , HGADPT(image.size.width), HGADPT(image.size.height))];
    [leftView addSubview:lefticon];
    lefticon.image =image;
    return leftView;
}
-(void)checkInputContent
{
    if ([self.mutable_checks containsObject:@"0"]) {
        nextBtn.backgroundColor = HGColor(221, 221, 221);
        nextBtn.userInteractionEnabled = NO;
    }
    else
    {
        nextBtn.backgroundColor = BTN_NORMAL_COLOR;
        nextBtn.userInteractionEnabled = YES;
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.mutable_checks replaceObjectAtIndex:textField.tag-120 withObject:textField.text.length==0?@"0":textField.text];
    
    if ([self.mutable_checks containsObject:@"0"]) {
        nextBtn.backgroundColor = HGColor(221, 221, 221);
        nextBtn.userInteractionEnabled = NO;
    }
    else
    {
        
        if ([[NSString stringWithFormat:@"%@",self.mutable_checks[0]] isEqualToString:[NSString stringWithFormat:@"%@",self.mutable_checks[1]]]) {
            nextBtn.backgroundColor = BTN_NORMAL_COLOR;
            nextBtn.userInteractionEnabled = YES;

        }
        else{
            [MBProgressHUD showSuccess:@"两次密码输入有误，请确认！"];
            nextBtn.backgroundColor = HGColor(221, 221, 221);
            nextBtn.userInteractionEnabled = NO;
        }
    }
    

}


@end
